package com.yc154.web;

import com.yc.damai.util.QRCodeUtil;
import com.yc154.po.Emp;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.io.OutputStream;
import java.util.List;

@RestController
public class UserDo {

    @RequestMapping("user")
    public Mono<String> user(ServerWebExchange exchange) {
        int port = exchange.getRequest().getLocalAddress().getPort();
        return Mono.just("user: " + port);
    }

    @Autowired
    WebClient.Builder builder;

    @RequestMapping("fromIndex")
    public Mono<String> index() {
        return builder.build()
                .get()
                .uri("http://index/index")
                .retrieve()
                .bodyToMono(String.class);
    }

    @RequestMapping("add")
    public Mono<Integer> add(int a, int b) {
        return Mono.just(a + b);
    }

    @RequestMapping("hello/{name}")
    public Mono<String> hello(@PathVariable("name") String name) {
        return Mono.just("hello " + name);
    }

    // 使用请求参数传参
    @RequestMapping("emp1")
    public Emp emp1(Emp emp) {
        emp.setAge(emp.getAge() + 10);
        return emp;
    }

    // 使用body传参
    @RequestMapping("emp2")
    public Emp emp2(@RequestBody Emp emp) {
        emp.setAge(emp.getAge() + 10);
        return emp;
    }

    // 消费者
    @RabbitListener(queuesToDeclare = @Queue("simple.queue"))  // queuesToDeclare 自动声明队列
    public void 一对一消费(String message) {
        System.out.println("message = " + message);
    }

    // 指定队列，可以接收缓存到队列里的消息
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = "pubSub", durable = "true"),
            exchange = @Exchange(value = "fanoutTest", type = ExchangeTypes.FANOUT))})
    public void fanoutTest1(String message) {
        System.out.println("fanoutTest1 = " + message);
    }

    // 不指定队列，消息过了就没了
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue,
            exchange = @Exchange(value = "fanoutTest", type = ExchangeTypes.FANOUT))})
    public void fanoutTest2(String message) {
        System.out.println("fanoutTest2 = " + message);
    }


    /// //////////////////////////////////////////////
    // 指定队列，可以接收缓存到队列里的消息
// key = {"info","error"} 表示我能接收到routingKey为 info和error的消息
    @RabbitListener(bindings = {@QueueBinding(value = @Queue(
            value = "route", durable = "true"),
            exchange = @Exchange(value = "direstTest",
                    type = ExchangeTypes.DIRECT), key = {"info", "error"})})
    public void route1(String message) {
        System.out.println("route1 = " + message);
    }

    // 不指定队列，消息过了就没了
// key = {"error"} 表示我只能接收到routingKey为 error的消息
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue,
            exchange = @Exchange(value = "direstTest",
                    type = ExchangeTypes.DIRECT), key = {"error"})})
    public void route2(String message) {
        System.out.println("route2 = " + message);
    }


    ///////////////////////////////////////
    /**
     * 代表一个单词，#可以代替零个或多个单词
     */
// 指定队列，可以接收缓存到队列里的消息
// key = {"user.save","user.*"}
// 表示能消费 routingkey为  user.save 和 user.任意一个字符  的消息
    @RabbitListener(bindings = {@QueueBinding(value = @Queue(
            value = "topic", durable = "true"),
            exchange = @Exchange(name = "topicTest",
                    type = ExchangeTypes.TOPIC), key = {"user.save", "user.*"})})
    public void recevicel(String message) {
        System.out.println("topic1 = " + message);
    }
    // 不指定队列，消息过了就没了
// key = {"order.#","user.*"}
// 表示能消费 routingkey为  order.一个或多个字符   和  user.任意一个字符  的消息
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue,
            exchange = @Exchange(name = "topicTest",
                    type = ExchangeTypes.TOPIC), key = {"order.#", "user.*"})})
    public void recevicel1(String message) {
        System.out.println("topic2 = " + message);
    }

    @Autowired
    JdbcTemplate jdbcTemplate;

    @RequestMapping("allUser")
    List allUser(){
        return jdbcTemplate.queryForList("select * from user");
    }

}
